home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Metafiles / MetafileViewer / MetafileViewer.cs next >
Encoding:
Text File  |  2001-01-15  |  6.2 KB  |  183 lines

  1. //---------------------------------------------
  2. // MetafileViewer.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.Drawing.Printing;
  8. using System.IO;              // For Path class
  9. using System.Windows.Forms;
  10.  
  11. class MetafileViewer: Form
  12. {
  13.      protected Metafile mf;
  14.      protected string   strProgName;
  15.      protected string   strFileName;
  16.      MenuItem           miFileSaveAs, miFilePrint, 
  17.                         miFileProps, miViewChecked;
  18.  
  19.      public static void Main()
  20.      {
  21.           Application.Run(new MetafileViewer());
  22.      }
  23.      public MetafileViewer()
  24.      {
  25.           Text = strProgName = "Metafile Viewer";
  26.           ResizeRedraw = true;
  27.  
  28.           Menu = new MainMenu();
  29.  
  30.                // File menu
  31.  
  32.           MenuItem mi = new MenuItem("&File");
  33.           mi.Popup += new EventHandler(MenuFileOnPopup);
  34.           Menu.MenuItems.Add(mi);
  35.  
  36.                // File Open menu item
  37.  
  38.           mi = new MenuItem("&Open...");
  39.           mi.Click += new EventHandler(MenuFileOpenOnClick);
  40.           mi.Shortcut = Shortcut.CtrlO;
  41.           Menu.MenuItems[0].MenuItems.Add(mi);
  42.  
  43.                // File Save As Bitmap menu item
  44.  
  45.           miFileSaveAs = new MenuItem("Save &As Bitmap...");
  46.           miFileSaveAs.Click += new EventHandler(MenuFileSaveAsOnClick);
  47.           Menu.MenuItems[0].MenuItems.Add(miFileSaveAs);
  48.           Menu.MenuItems[0].MenuItems.Add("-");
  49.  
  50.                // File Print menu item
  51.  
  52.           miFilePrint = new MenuItem("&Print...");
  53.           miFilePrint.Click += new EventHandler(MenuFilePrintOnClick);
  54.           Menu.MenuItems[0].MenuItems.Add(miFilePrint);
  55.           Menu.MenuItems[0].MenuItems.Add("-");
  56.  
  57.                // File Properties menu item
  58.  
  59.           miFileProps = new MenuItem("Propert&ies...");
  60.           miFileProps.Click += new EventHandler(MenuFilePropsOnClick);
  61.           Menu.MenuItems[0].MenuItems.Add(miFileProps);
  62.  
  63.                // Edit menu (temporary until Chapter 24)
  64.  
  65.           Menu.MenuItems.Add("&Edit");
  66.  
  67.                // View menu
  68.  
  69.           Menu.MenuItems.Add("&View");
  70.  
  71.           string[] astr = { "&Stretched to Window", 
  72.                             "&Metrical Size", "&Pixel Size" };
  73.           EventHandler eh = new EventHandler(MenuViewOnClick);
  74.  
  75.           foreach (string str in astr)
  76.                Menu.MenuItems[2].MenuItems.Add(str, eh);
  77.  
  78.           miViewChecked = Menu.MenuItems[2].MenuItems[0];
  79.           miViewChecked.Checked = true;
  80.      }
  81.      void MenuFileOnPopup(object obj, EventArgs ea)
  82.      {
  83.           miFileSaveAs.Enabled = 
  84.           miFilePrint.Enabled =  
  85.           miFileProps.Enabled = (mf != null);
  86.      }
  87.      void MenuFileOpenOnClick(object obj, EventArgs ea)
  88.      {
  89.           OpenFileDialog dlg = new OpenFileDialog();
  90.  
  91.           dlg.Filter = "All Metafiles|*.wmf;*.emf|" +
  92.                        "Windows Metafile (*.wmf)|*.wmf|" +
  93.                        "Enhanced Metafile (*.emf)|*.emf|" +
  94.                        "All files|*.*";
  95.  
  96.           if (dlg.ShowDialog() == DialogResult.OK)
  97.           {
  98.                try
  99.                {
  100.                     mf = new Metafile(dlg.FileName);
  101.                }
  102.                catch (Exception exc)
  103.                {
  104.                     MessageBox.Show(exc.Message, strProgName);
  105.                     return;
  106.                }
  107.                strFileName = dlg.FileName;
  108.                Text = strProgName + " - " + Path.GetFileName(strFileName);
  109.                Invalidate();
  110.           }
  111.      }
  112.      protected virtual void MenuFileSaveAsOnClick(object obj, EventArgs ea)
  113.      {
  114.           MessageBox.Show("Not yet implemented!", strProgName);
  115.      }
  116.      void MenuFilePrintOnClick(object obj, EventArgs ea)
  117.      {
  118.           PrintDocument prndoc = new PrintDocument();
  119.  
  120.           prndoc.DocumentName = Text;
  121.           prndoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
  122.           prndoc.Print();
  123.      }
  124.      void MenuFilePropsOnClick(object obj, EventArgs ea)
  125.      {
  126.           MetafileHeader mh = mf.GetMetafileHeader();
  127.  
  128.           string str = 
  129.                "Image Properties" +
  130.                "\n\tSize = " + mf.Size + 
  131.                "\n\tHorizontal Resolution = " + mf.HorizontalResolution +
  132.                "\n\tVertical Resolution = " + mf.VerticalResolution +
  133.                "\n\tPhysical Dimension = " + mf.PhysicalDimension +
  134.                "\n\nMetafile Header Properties" +
  135.                "\n\tBounds = " + mh.Bounds +
  136.                "\n\tDpiX = " + mh.DpiX +
  137.                "\n\tDpiY = " + mh.DpiY +
  138.                "\n\tLogicalDpiX = " + mh.LogicalDpiX +
  139.                "\n\tLogicalDpiY = " + mh.LogicalDpiY +
  140.                "\n\tType = " + mh.Type +
  141.                "\n\tVersion = " + mh.Version +
  142.                "\n\tMetafileSize = " + mh.MetafileSize;
  143.  
  144.           MessageBox.Show(str, Text);
  145.      }
  146.      void MenuViewOnClick(object obj, EventArgs ea)
  147.      {
  148.           miViewChecked.Checked = false;
  149.           miViewChecked = (MenuItem) obj;
  150.           miViewChecked.Checked = true;
  151.           Invalidate();
  152.      }
  153.      void OnPrintPage(object obj, PrintPageEventArgs ppea)
  154.      {
  155.           Graphics  grfx = ppea.Graphics;
  156.           Rectangle rect = new Rectangle(
  157.                ppea.MarginBounds.Left -
  158.                (ppea.PageBounds.Width - 
  159.                     (int) grfx.VisibleClipBounds.Width) / 2,
  160.                ppea.MarginBounds.Top -
  161.                (ppea.PageBounds.Height - 
  162.                     (int) grfx.VisibleClipBounds.Height) / 2,
  163.                ppea.MarginBounds.Width,
  164.                ppea.MarginBounds.Height);
  165.  
  166.           DisplayMetafile(grfx, rect);
  167.      }
  168.      protected override void OnPaint(PaintEventArgs pea)
  169.      {
  170.           if (mf != null)
  171.                DisplayMetafile(pea.Graphics, ClientRectangle);
  172.      }
  173.      void DisplayMetafile(Graphics grfx, Rectangle rect)
  174.      {
  175.           switch (miViewChecked.Index)
  176.           {
  177.           case 0: grfx.DrawImage(mf, rect);  break;
  178.           case 1: grfx.DrawImage(mf, rect.X, rect.Y);  break;
  179.           case 2: grfx.DrawImage(mf, rect.X, rect.Y, mf.Width, mf.Height);
  180.                   break;
  181.           }
  182.      }
  183. }